package com.duolaibei.business.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.duolaibei.business.R;
import com.duolaibei.business.api.ApiName;
import com.duolaibei.business.base.BaseActivity;
import com.duolaibei.business.config.Constant;
import com.duolaibei.business.config.Preferences;
import com.duolaibei.business.http.HttpUtils;
import com.duolaibei.business.model.ResponseResult;
import com.duolaibei.business.utils.CommonUtil;
import com.duolaibei.business.utils.GsonUtil;
import com.duolaibei.business.utils.PreferenceUtil;
import com.duolaibei.business.utils.cutdown.CountDownTimerUtil;
import com.duolaibei.business.utils.inject.Inject;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by xlm on 2017/10/27 0027
 */

public class RegistAndForgetActivity extends BaseActivity {

    private int type;
    @Inject(R.id.forget_regist_invite)
    private LinearLayout mLayoutRegistInvite;
    @Inject(R.id.forget_regist_next)
    private TextView mTextViewNext;
    @Inject(R.id.forget_regist_login)
    private TextView mTextViewLogin;
    @Inject(R.id.forget_regist_change)
    private TextView mTextViewChange;
    @Inject(R.id.title)
    private TextView mViewTitle;
    @Inject(R.id.regist_divider)
    private View mViewDivider;
    @Inject(R.id.forget_regist_getyzcode)
    private TextView mTextViewCutDown;

    private EditText mEditTextMobile;
    private EditText mEditTextYzCode;
    private EditText mEditTextPassWord;
    private EditText mEditTextInviteCode;
    private EditText mEditTextComfirm;

    @Override
    protected void onGetIntent(Intent intent) {
        super.onGetIntent(intent);
        type = intent.getIntExtra(Constant.LOGIN_MODE, -1);
    }

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_forget_regist);
    }

    @Override
    protected void initViews() {
        mEditTextMobile = (EditText) findViewById(R.id.forget_regist_mobile);
        mEditTextYzCode = (EditText) findViewById(R.id.forget_regist_yzcode);
        mEditTextPassWord = (EditText) findViewById(R.id.forget_regist_password);
        mEditTextInviteCode = (EditText) findViewById(R.id.regist_invite);
        mEditTextComfirm = (EditText) findViewById(R.id.forget_regist_password_comfirm);
        if (Constant.MODE_FORGET == type) {
            mLayoutRegistInvite.setVisibility(View.GONE);
            mTextViewNext.setVisibility(View.GONE);
            mTextViewLogin.setVisibility(View.GONE);
            mViewDivider.setVisibility(View.GONE);
            mViewTitle.setText("忘记密码");
        } else if (Constant.MODE_REGIST == type) {
            mTextViewChange.setVisibility(View.GONE);
            mViewTitle.setText("商户注册");
        }
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {

    }

    @Override
    protected void onSetListener() {
        mTextViewChange.setOnClickListener(this);
        mTextViewLogin.setOnClickListener(this);
        mTextViewCutDown.setOnClickListener(this);
        mTextViewNext.setOnClickListener(this);
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.forget_regist_login) {
            Intent intentLogin = new Intent(getThis(), LoginActivity.class);
            intentLogin.putExtra(Constant.LOGIN_MODE, Constant.MODE_BOSS);
            startActivity(intentLogin);
        } else if (v.getId() == R.id.forget_regist_getyzcode) {
            getSmsData();
        } else if (v.getId() == R.id.forget_regist_next) {
            getRegistData();
        } else if (v.getId() == R.id.forget_regist_change) {
            resetPassWord();
        }
    }

    /**
     * 获取注册信息
     */
    private void getRegistData() {
        final String mobile = mEditTextMobile.getText().toString();
        String yzcode = mEditTextYzCode.getText().toString();
        final String password = mEditTextPassWord.getText().toString();
        String invitecode = mEditTextInviteCode.getText().toString();
        String confirm = mEditTextComfirm.getText().toString();
        if (!CommonUtil.isMobile(mobile)) {
            showToast("请输入手机号码");
            return;
        }
        if (CommonUtil.isEmpty(yzcode)) {
            showToast("请输入验证码");
            return;
        }
        if (CommonUtil.isEmpty(password)) {
            showToast("请输入密码");
            return;
        }
        if (CommonUtil.isEmpty(confirm)) {
            showToast("请再次输入密码");
            return;
        }
        if (CommonUtil.isEmpty(invitecode)) {
            invitecode = "40";
//            showToast("请输入注册码");
//            return;
        }
        if (!password.equals(confirm)) {
            showToast("密码输入不一致");
            return;
        }
        if (!CommonUtil.isMobile(mobile)) {
            showToast("手机号码不正确");
            return;
        }
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", ApiName.REGISTER);
        param.add("reg_type", "mobile");
        param.add("tg", "0");
        param.add("ag", invitecode);
        param.add("user_type", "2");
        param.add("mobile", mobile);
        param.add("verify", yzcode);
        param.add("password", password);
        param.add("sign", param.getMd5String());
        showLoading("注册中");
        HttpUtils.getInstance().url(ApiName.API).params(param)
                .executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                dismissLoading();
                if (CommonUtil.isForeground(getThis())) {
                    showToast(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                dismissLoading();
                if (HttpUtils.isJson(string)) {
                    ResponseResult result = GsonUtil
                            .GsonToBean(string, ResponseResult.class);
                    if (result.getStatus() == 1) {
                        //注册成功  跳转商户登录界面
                        Intent intent = new Intent();
                        intent.putExtra(Constant.LOGIN_MOBILE, mobile);
                        intent.putExtra(Constant.LOGIN_PASSWORD, password);
                        intent.putExtra(Constant.LOGIN_MODE, Constant.MODE_BOSS);
                        intent.setClass(getThis(), LoginActivity.class);
                        startActivity(intent);
                    }
                    showToast(result.getInfo());
                } else {
                    showToast(string);
                }
            }
        });

    }


    /**
     * 获取短信验证码
     */
    private void getSmsData() {
        String mobile = mEditTextMobile.getText().toString();
        if (!CommonUtil.isMobile(mobile)) {
            showToast("请输入手机号码");
            return;
        }
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", ApiName.SENDSMS);
        param.add("mobile", mobile);
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().url(ApiName.API)
                .params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    try {
                        showToast(new JSONObject(string).optString("info"));
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                new CountDownTimerUtil(mTextViewCutDown,
                                        30000, 1000).start();
                            }
                        });
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    showToast(string);
                }
            }
        });
    }

    /**
     * 重置密码
     */
    private void resetPassWord() {
        final String mobile = mEditTextMobile.getText().toString();
        String yzcode = mEditTextYzCode.getText().toString();
        final String password = mEditTextPassWord.getText().toString();
        if (!CommonUtil.isMobile(mobile)) {
            showToast("请输入手机号码");
            return;
        }
        if (CommonUtil.isEmpty(yzcode)) {
            showToast("请输入验证码");
            return;
        }
        if (CommonUtil.isEmpty(password)) {
            showToast("请输入密码");
            return;
        }
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", ApiName.RESET_PASSWORD);
        param.add("username", mobile);
        param.add("password", password);
        param.add("verify", yzcode);
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().url(ApiName.API)
                .params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    ResponseResult result = GsonUtil
                            .GsonToBean(string, ResponseResult.class);
                    if (result.getStatus() == 1) {
                        //修改密码成功
                        Intent intent = new Intent();
                        intent.putExtra(Constant.LOGIN_MOBILE, mobile);
                        intent.putExtra(Constant.LOGIN_PASSWORD, password);
                        //保存密码
                        PreferenceUtil.setParam(Preferences.PASSWORD, password);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                    showToast(result.getInfo());
                } else {
                    showToast(string);
                }
            }
        });
    }

}
